<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/*
	Copyright (c) 2011 Aditya Satrya

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:
	
	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.
	
	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

/*
	File: MY Application Helper

	Pulls together various helper functions from across the core modules
	to ease editing and minimize physical files that need loaded.
*/

//--------------------------------------------------------------------

//--------------------------------------------------------------------
// !CONTEXT HELPERS
//--------------------------------------------------------------------

/*
	Function: context_nav_arr()
	
	Builds the navigation array for the main contexts
	list. 
	
	Returns:
		An array
*/
if (!function_exists('context_nav_arr'))
{
	function context_nav_arr()
	{ 
		$contexts = config_item('contexts');
		
		if (empty($contexts) || !is_array($contexts) || !count($contexts))
		{
			die(lang('bf_no_contexts'));
		}
		
		// Ensure settings context exists
		if (!in_array('settings', $contexts))
		{
			array_push($contexts, 'settings');
		}
		
		// Ensure developer context exists
		if (!in_array('developer', $contexts))
		{
			array_push($contexts, 'developer');
		}

		$nav = array();
		
		/*
			Build out our navigation array.
		*/
		foreach ($contexts as $context)
		{	
			if (has_permission('Site.'. ucfirst($context) .'.View'))
			{	
				$nav[$context]['url'] = site_url(SITE_AREA .'/'.$context);
				$nav[$context]['class'] = check_class($context);
				$nav[$context]['id'] = 'tb_'. $context;
				$nav[$context]['title'] = lang('bf_context_'. $context);
			}
		}
		
		return $nav;
	}
}

/*
	Function: context_subnav_arr()
	
	Builds the sub navigation array for the main contexts
	list. 
	
	Returns:
		An array
*/
if (!function_exists('context_subnav_arr'))
{
	function context_subnav_arr($context)
	{ 
		$actions = array();
		$subnav_arr = array();
		
		// Get a list of modules with a controller matching
		// $context ('content', 'appearance', 'settings', 'statistics', or 'developer')
		foreach (module_list() as $module)
		{
			if (module_controller_exists($context, $module))
			{
				$actions[] = $module;
			}
		}
		
		// Do we have any actions? 
		if (!count($actions))
		{
			return $subnav_arr;
		}
		
		// Grab our module permissions so we know who can see what on the sidebar
		$permissions = config_item('module_permissions');
		
		// Build up our menu array
		foreach ($actions as $module)
		{
			// Make sure the user has permission to view this page.
			if ((isset($permissions[$context][$module]) && has_permission($permissions[$context][$module])) || !array_key_exists($module, $permissions[$context]))
			{	
				// Grab our module config array, if any.
				$mod_config = module_config($module);
				
				$display_name = isset($mod_config['name']) ? $mod_config['name'] : $module;
				$title = isset($mod_config['description']) ? $mod_config['description'] : $module;
				
				$menu_topic = isset($mod_config['menu_topic'][$context]) ? $mod_config['menu_topic'][$context] : $display_name;
				
				$menu_weight = isset($mod_config['menu_weight']) ? $mod_config['menu_weight'] : 999;
				
				// Drop-down menus?
				if (isset($mod_config['menus']) && isset($mod_config['menus'][$context]))
				{ 
					$menu_view = $mod_config['menus'][$context];
				} else
				{
					$menu_view = '';
				}
				
				$subnav_arr[$menu_topic][$module] = array(
						'title'			=> $title,
						'display_name'	=> $display_name,
						'menu_view'		=> $menu_view,
						'menu_topic'	=> $menu_topic,
						'menu_weight'	=> $menu_weight,
				);
			}
		}
		
		usort($subnav_arr, "cmp_menu_weight");
		
		//echo "<pre>";die(print_r($this->menu));
		//dump($subnav_arr);
		return $subnav_arr;
	}
}

if ( ! function_exists('cmp_menu_weight'))
{
	function cmp_menu_weight($a, $b)
	{
		reset($a);
		reset($b);
		$first_key_a = key($a);
		$first_key_b = key($b);
		if($a[$first_key_a]['menu_weight'] == $b[$first_key_b]['menu_weight']){
			return 0;
		}elseif($a[$first_key_a]['menu_weight'] < $b[$first_key_b]['menu_weight']){
			return -1;
		}else{
			return 1;
		}
	}
}